import requests
import re









class PenBook:
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
    penbook_url = 'https://www.biqugexsw.com'
    def __init__(self,keyword = None):

        if keyword != None:
            self.html = self.pensearch(keyword)
        else:
            pass

    def pensearch(self,keyword):

        params = {
            'ie': 'gbk',
            's': '2758772450457967865',
            'q': keyword
        }
        url = 'https://www.biqugexsw.com/s.php'
        html = requests.get(url,headers = self.headers,params = params)
        html.encoding = 'gbk'
        return html.text

    def searchinfo(self):


        book_name = '<h4 class="bookname"><a href="(.*?)">(.*?)</a></h4>'
        novel_author = '<div class="author">作者：(.*?)</div>'
        latest_chapter = '<div class="update"><span>最新章节：</span><a href="(.*?)">(.*?)</a></div>'
        novel_type = '<div class="cat">分类：(.*?)</div>'
        refresh_time = None
        novel_status = None
        novel_words = None
        novel_img = '<div class="bookimg"><a href=".*?"><img src="(.*?)"></a></div>'
        book_name = re.findall(book_name, self.html)
        novel_author = re.findall(novel_author, self.html)
        latest_chapter = re.findall(latest_chapter, self.html)
        novel_type = re.findall(novel_type, self.html)
        novel_img = re.findall(novel_img, self.html)
        # print(len(book_name))
        # print(len(novel_author))
        # print(len(latest_chapter))
        # print(len(novel_type))
        # print(len(novel_img))

        search_dict = {}

        for i in range(len(novel_img)):
            info_dict = {
                'novel_author':novel_author[i],
                'novel_author_url':None,
                'novel_url':self.penbook_url+book_name[i][0],
                'latest_chapter':latest_chapter[i][1],
                'latest_chapter_url':self.penbook_url+latest_chapter[i][0],
                'novel_type':novel_type[i],
                'refresh_time':None,
                'novel_status':None,
                'novel_words':None,
                'novel_img':self.penbook_url+novel_img[i]
            }
            search_dict[book_name[i][1]] = info_dict

        return search_dict


    def penhome(self):

        html = requests.get(self.penbook_url,headers = self.headers)
        html.encoding = 'gbk'
        html = html.text
        info = '<div class="image"><a href="(.*?)" target="_blank"><img src="(.*?)" alt="(.*?)">'
        novel_intro = '</a></dt><dd>(.*?)</dd></dl></div></div>'
        novel_author = '<dt><span>(.*?)</span>'
        info = re.findall(info, html)[:4]
        novel_intro = re.findall(novel_intro, html, re.S)
        novel_author = re.findall(novel_author, html)
        home_dict = {}
        for i in range(len(info)):
            info_dict = {
                'novel_author': novel_author[i],
                'novel_author_url': None,
                'novel_url': self.penbook_url + info[i][0],
                'novel_intro':novel_intro[i],
                'novel_type':None,
                'refresh_time':None,
                'novel_status': None,
                'novel_words': None,
                'novel_img': self.penbook_url + info[i][1],
                'latest_chapter':None,
                'latest_chapter_url':None
            }
            home_dict[info[i][2]] = info_dict


        return home_dict

    def pennovel(self,novel_url):
        html = requests.get(novel_url)
        html.encoding = 'gbk'
        html = html.text
        novel_name = '<meta property="og:novel:book_name" content="(.*)"/>'
        novel_author = '<meta property="og:novel:author" content="(.*)"/>'
        novel_intro = '<div class="intro"><span>简介：</span>(.*)<br>作者'
        novel_type = '<meta property="og:novel:category" content="(.*)"/>'
        refresh_time = '<meta property="og:novel:update_time" content="(.*)"/>'
        novel_status = '<meta property="og:novel:status" content="(.*)"/>'
        novel_words = '<span>字数：(.*)</span><span class="last">更新时间'
        novel_img = '<meta property="og:image" content="(.*)"/>'
        latest_chapter = '<meta property="og:novel:latest_chapter_name" content="(.*)"/>'
        latest_chapter_url = '<meta property="og:novel:latest_chapter_url" content="(.*)"/>'
        novel_chapter_info = '<dd><a href ="(.*?)">(.*?)</a></dd>'
        novel_name = re.findall(novel_name,html)[0]
        novel_author = re.findall(novel_author, html)[0]
        novel_intro = re.findall(novel_intro, html,re.S)[0]
        novel_type = re.findall(novel_type, html)[0]
        refresh_time = re.findall(refresh_time, html)[0]
        novel_status = re.findall(novel_status, html)[0]
        novel_words = re.findall(novel_words, html)[0]
        novel_img = re.findall(novel_img, html)[0]
        latest_chapter = re.findall(latest_chapter, html)[0]
        latest_chapter_url = re.findall(latest_chapter_url, html)[0]
        novel_chapter_info = re.findall(novel_chapter_info,html)[6:]
        chapter_url_list = []
        chapter_list = []
        for i in novel_chapter_info:
            chapter_url_list.append(self.penbook_url+i[0])
            chapter_list.append(i[1])
        novel_chapter_info = dict(zip(chapter_list,chapter_url_list))

        novel_dict = {
            'novel_name': novel_name,
            'novel_author': novel_author,
            'novel_author_url': None,
            'novel_intro': novel_intro,
            'novel_type': novel_type,
            'refresh_time': refresh_time,
            'novel_status': novel_status,
            'novel_words': novel_words,
            'novel_img': novel_img,
            'latest_chapter': latest_chapter,
            'latest_chapter_url': latest_chapter_url,
            'novel_chapter_info': novel_chapter_info
        }
        return novel_dict


    def novel_chapter(self,chapter_url):
        html = requests.get(chapter_url)
        html.encoding = 'gbk'
        html = html.text
        chapter_content = '<div id="content" class="showtxt">(.*)</div>'
        info = "<script>lastread.set(.*);</script>"
        novel_catalog = '<li><a href="(.*)">返回目录</a></li>'
        pre_chapter = '<li><a href="(.*)">上一章</a></li>'
        next_chapter = '<li><a href="(.*)">下一章</a></li>'
        info = eval(re.findall(info, html)[0])
        chapter_content = re.findall(chapter_content, html)
        chapter_content = str(chapter_content).replace('[', '').replace(']', '').replace(
            chapter_url+'<br /><br />',
            '').replace('请记住本书首发域名：www.biqugexsw.com。笔趣阁小说网手机版阅读网址：m.biqugexsw.com','')
        novel_catalog = re.findall(novel_catalog,html)[0]
        pre_chapter = re.findall(pre_chapter, html)[0]
        next_chapter = re.findall(next_chapter, html)[0]
        chapter_info = {
            'chapter_name': info[3],
            'chapter_content': chapter_content,
            'novel_name': info[2],
            'novel_author': info[4],
            'novel_words': None,
            'novel_catalog': novel_catalog,
            'pre_chapter': pre_chapter,
            'next_chapter': next_chapter
        }

        return chapter_info



if __name__ == '__main__':
    # x = PenBook(keyword='飞鸟')
    # print(x.searchinfo())
    x = PenBook()
    # print(x.novel_chapter(chapter_url = 'https://www.biqugexsw.com/3_3568/2536689.html'))
    # print(x.penhome())
    print(x.pennovel(novel_url='https://www.biqugexsw.com/3_3568/'))






